$("document").ready(function(){

    
    $("#header #main_menu .demo_video").each(function(){
		var videoURL = $(this).attr("rel");
		var videoID = getParameterByName2("v", videoURL);
		if(videoID != ""){
			$(this).addClass("featuredVideo").data("videoID", videoID).append("<img src='http://img.youtube.com/vi/" + videoID + "/0.jpg' alt='' width='200' />");
		}
	});
	
	$("#footer #payments").append("<iframe src='https://www.pagamentodigital.com.br/site/selo_reputacao.php?chave_primeira=831521&chave_segunda=1926768&chave_terceira=90x95' width='90' height='95' scrolling='no' frameborder='0' marginheight='0' marginwidth='0'></iframe>");
	
	$('.productImage').prepend('<div id="imgTeste" />');
	
	$(".prateleira li .productImage").click(function(){
		$(this).children('div').hide();
	});
	
	$('#image').prepend('<div id="imgTeste" />');
	
	$("#image").click(function(){
		$(this).children('div').hide();
	});
	
	
	$("#header #main_menu > ul > li > ul").each(function(){
		var negativeLeft = ($(this).width() / 2) * (-1);
		$(this).css("margin-left", negativeLeft);
	});
	
	$("#header #main_menu ul li").hover(function(){
		$(this).find(" > ul").css("display", "block");
	}, function(){
		$(this).find(" > ul").css("display", "none");
		
		if($(this).find(".featuredVideo").find("object").length > 0){
			var videoID = $(this).find(".featuredVideo").data("videoID");
			$(this).find(".featuredVideo").find("object").remove();
			$(this).find(".featuredVideo").append("<img src='http://img.youtube.com/vi/" + videoID + "/0.jpg' alt='' width='200' />");
		}
	});

	$("#social_links li").hover(function(){
		$(this).stop().animate({"margin-top":"-15px"}, 500);
	}, function(){
		$(this).stop().animate({"margin-top":"0"}, 500, 'easeOutBounce');
	});
	
	$(".prateleira > ul > li").each(function(){
		var videoURL = $(this).find(".product_field_6 li:eq(0)").text();
		var videoID = getParameterByName2("v", videoURL);
		if(videoID != ""){
			$(this).find(".productImage").find("img").remove();
			$(this).find(".productImage").addClass("featuredVideo").data("videoID", videoID).append("<img src='http://img.youtube.com/vi/" + videoID + "/0.jpg' alt='' width='260' />");
		}
	});
	
	$("#caracteristicas").each(function(){
		var videoURL = $(this).find("td.value-field.video_url").text();
		var videoID = getParameterByName2("v", videoURL);
		if(videoID != ""){
			$('.apresentacao').find("#image").find("img").remove();
			$('.apresentacao').find("#image").addClass("featuredVideo").data("videoID", videoID).append("<img src='http://img.youtube.com/vi/" + videoID + "/0.jpg' alt='' width='260' />");
		}
	});
	
	$(".featuredVideo").live("click", function(){
		var playerCount = $("object").length;
		var playerId = "YTPlayer_" + playerCount;
		var videoID = $(this).data("videoID");
		
		var params = { allowScriptAccess: "always", wmode: "transparent" };
		var atts = { id: playerId };
		var width = $(this).find("img").width();
		var height = $(this).find("img").height();
		$(this).find("img").remove();
		$(this).append("<div id='" + playerId + "' />");
		swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "?enablejsapi=1&color1=0x000000&color2=0xED1C24&autoplay=1&rel=0&showinfo=0&playerapiid=" + playerId, playerId, width, height, "8", null, null, params, atts);
		
		return false;
	});
    
    if($("body").is(".produto")){
		
		$('.gmais').html('<g:plusone size="small"></g:plusone>');
	}
    
});

function getParameterByName2(name, url){
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(url);
	if(results == null)
		return "";
	else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
}

$(document).ajaxComplete(function(event,request, settings){
	if($("body").is(".resultado-busca")){
        var marginNeg=$('.collectionWrap .pager .pages').width();
        $('.collectionWrap .pager .pages').css("margin-left", (marginNeg)/2 * (-1));
    }
	
	if ($("body").attr("class") == "produto"){
		var videoURL = $("td.value-field.video_url").text();
		var videoID = getParameterByName2("v", videoURL);
		if(videoID != ""){
			$('.apresentacao').find("#image").find("img").remove();
			$('.apresentacao').find("#image").addClass("featuredVideo").data("videoID", videoID).append("<img src='http://img.youtube.com/vi/" + videoID + "/0.jpg' alt='' width='420' />");
		}
	};
});